package com.example.petstore_2.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import okhttp3.*;

import java.io.IOException;
import java.net.URLEncoder;

public class GiteeLogin {

    private static final String client_id = "ba92f844c06bf93080ad7fc1b86468a002305bc1a859e753eaffdf0d712b9351";
    private static final String redirect_uri_login = "http://localhost:5173/giteeLogin";
    private static final String redirect_uri_register = "http://localhost:5173/giteeRegister";
    private static final String client_secret = "290b71cf57ecbb4e60b47f34465c98c0ff658910cf58bb1eafb9af7a0316680e";
    private static String getAccessKey(String code, String type) {
        OkHttpClient client = new OkHttpClient();
        // 通过该地址能够获取到access_token
        String url;
        if(type.equals("login")) {
            url = "https://gitee.com/oauth/token?grant_type=authorization_code&" +
                    "&code=" + code +
                    "&client_id=" + client_id +
                    "&redirect_uri=" + URLEncoder.encode(redirect_uri_login) +
                    "&client_secret=" + client_secret;
        }else{
            url = "https://gitee.com/oauth/token?grant_type=authorization_code&" +
                    "&code=" + code +
                    "&client_id=" + client_id +
                    "&redirect_uri=" + URLEncoder.encode(redirect_uri_register) +
                    "&client_secret=" + client_secret;
        }
        RequestBody requestBody = new FormBody.Builder()
                    .add("client_secret", client_secret)
                    .build();

        Request request = new Request.Builder()
                .post(requestBody)
                .url(url).build();

        String accessKey = "";
        try {
            Response response = client.newCall(request).execute();
            String json = response.body().string();
            System.out.println(json);
            JSONObject jsonObject =  JSON.parseObject(json);
            // 获取json串中的access_token属性
            accessKey= (String) jsonObject.get("access_token");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return accessKey;
    }

    public static String getUserInfo(String code, String type) {
        String json = "";
        OkHttpClient client = new OkHttpClient();
        String accessKey = getAccessKey(code, type);
        // 通过该地址能够获取到用户信息
        String url = "https://gitee.com/api/v5/user?access_token=" + accessKey;
        Request request = new Request.Builder()
                .get()
                .url(url).build();
        try {
            Response response = client.newCall(request).execute();
            json = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

    public static String getLoginForm(){
        String url = "https://gitee.com/oauth/authorize?response_type=code" +
                "&client_id=" + client_id +
                "&redirect_uri=" + URLEncoder.encode(redirect_uri_login);
        return url;
    }
    public static String getReigsterForm(){
        String url = "https://gitee.com/oauth/authorize?response_type=code" +
                "&client_id=" + client_id +
                "&redirect_uri=" + URLEncoder.encode(redirect_uri_register);
        return url;
    }
}
